<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>实例事件</title>
    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>

<body>
    <h1>实例事件</h1>
    <hr>
    <div id="app">
        <p>{{num}}</p>
        <p><button @click="add">Add</button></p>
    </div>
    <p><button onclick="reduce()">reduce</button></p>
    <p><button onclick="reduceOnce()">reduceOnce</button></p>
    <p><button onclick="off()">off</button></p>
    <script type="text/javascript">
        var app = new Vue({
            el: '#app',
            data: {
                num: 1
            },
            methods: {
                add: function () {
                    var t = this;
                    t.num++
                }
            }
        })
        app.$on("reduce", function () {
            var t = this;
            console.log("执行了reduce方法")
            t.num--
        })
        app.$once("reduceOnce", function () {
            var t = this;
            console.log("只执行一次的方法")
            t.num--
        })

        function reduce() {
            app.$emit('reduce');
        }

        function reduceOnce() {
            app.$emit('reduceOnce'); //$emit 调用
        }
        function off() {
            console.log("关闭了reduce方法")
            app.$off('reduce');
        }
    </script>
</body>

</html>